package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.IssueSalaryCalculateRuleDTO;
import org.jsola.hr.dto.SalaryCalculateRuleGroupAddDTO;
import org.jsola.hr.dto.SalaryCalculateRuleGroupUpdateDTO;
import org.jsola.hr.query.SalaryCalculateRuleEmpInfoQuery;
import org.jsola.hr.query.SalaryCalculateRuleGroupQuery;
import org.jsola.hr.service.ISalaryCalculateRuleGroupService;
import org.jsola.hr.vo.EmpInfoVO;
import org.jsola.hr.vo.SalaryCalculateRuleGroupListVO;
import org.jsola.hr.vo.SalaryCalculateRuleGroupVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 工价计算规则组
 *
 * @author zhr
 */
@RestController("hrSalaryCalculateRuleGroupController")
@RequestMapping(HrConstants.API_PATH + "/v1/salaryCalculateRuleGroups")
public class SalaryCalculateRuleGroupController implements ISalaryCalculateRuleGroupControllerAPI {

    @Autowired
    private ISalaryCalculateRuleGroupService salaryCalculateRuleGroupService;

    @Override
    @PostMapping
    public Result<SalaryCalculateRuleGroupVO> save(@Valid @RequestBody SalaryCalculateRuleGroupAddDTO salaryCalculateRuleGroupAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增工价计算规则组权限校验

        return Result.success(
                salaryCalculateRuleGroupService.save(salaryCalculateRuleGroupAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping(value = "/issueGroup")
    public Result<Integer> issueGroup(@Valid @RequestBody IssueSalaryCalculateRuleDTO issueSalaryCalculateRuleDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增工价计算规则组权限校验

        return Result.success(
                salaryCalculateRuleGroupService.issueGroup(issueSalaryCalculateRuleDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SalaryCalculateRuleGroupUpdateDTO salaryCalculateRuleGroupUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工价计算规则组权限校验

        int count = salaryCalculateRuleGroupService.update(salaryCalculateRuleGroupUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{salaryCalculateRuleGroupId}")
    public Result<Integer> delete(@PathVariable String salaryCalculateRuleGroupId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除工价计算规则组权限校验

        int count = salaryCalculateRuleGroupService.deleteByIds(tokenUser, salaryCalculateRuleGroupId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] salaryCalculateRuleGroupIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除工价计算规则组权限校验

        int count = salaryCalculateRuleGroupService.deleteByIds(tokenUser, salaryCalculateRuleGroupIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{salaryCalculateRuleGroupId}")
    public Result<SalaryCalculateRuleGroupVO> findById(@PathVariable String salaryCalculateRuleGroupId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工价计算规则组权限校验

        return Result.success(
            salaryCalculateRuleGroupService.selectById(salaryCalculateRuleGroupId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SalaryCalculateRuleGroupListVO>> listAll(@Valid SalaryCalculateRuleGroupQuery salaryCalculateRuleGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工价计算规则组权限校验

        List<SalaryCalculateRuleGroupListVO> salaryCalculateRuleGroupListVOList =  salaryCalculateRuleGroupService.select(salaryCalculateRuleGroupQuery, tokenUser.getSiteId());
        // 需要展示使用人数则查询使用人数
        if (salaryCalculateRuleGroupQuery.getIsShowPeopleNumber() != null && salaryCalculateRuleGroupQuery.getIsShowPeopleNumber()) {
            salaryCalculateRuleGroupListVOList.forEach(salaryCalculateRuleGroupListVO ->
                    salaryCalculateRuleGroupService.setUsePeopleNumber(salaryCalculateRuleGroupListVO, tokenUser));
        }
        return Result.success(
                salaryCalculateRuleGroupListVOList
        );
    }


    @Override
    @GetMapping
    public Result<Page<SalaryCalculateRuleGroupListVO>> page(@Valid SalaryCalculateRuleGroupQuery salaryCalculateRuleGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        Page<SalaryCalculateRuleGroupListVO> page = salaryCalculateRuleGroupService.selectPage(salaryCalculateRuleGroupQuery, tokenUser.getSiteId());
        // 需要展示使用人数则查询使用人数
        if (salaryCalculateRuleGroupQuery.getIsShowPeopleNumber() != null && salaryCalculateRuleGroupQuery.getIsShowPeopleNumber()) {
            page.getEntities().forEach(salaryCalculateRuleGroupListVO ->
                    salaryCalculateRuleGroupService.setUsePeopleNumber(salaryCalculateRuleGroupListVO, tokenUser));
        }
        return Result.success(
                page
        );
    }

    @Override
    @GetMapping("/selectEmpInfoVoBySalaryCalculateRuleGroupId")
    public Result<Page<EmpInfoVO>> selectEmpInfoVoCompanyUse(@Valid SalaryCalculateRuleEmpInfoQuery salaryCalculateRuleEmpInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                salaryCalculateRuleGroupService.selectEmpInfoVoCompanyUse(salaryCalculateRuleEmpInfoQuery, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectEmpInfoVoSiteUse")
    public Result<Page<EmpInfoVO>> selectEmpInfoVoSiteUse(@Valid SalaryCalculateRuleEmpInfoQuery salaryCalculateRuleEmpInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                salaryCalculateRuleGroupService.selectEmpInfoVoSiteUse(salaryCalculateRuleEmpInfoQuery, tokenUser)
        );
    }



}

